﻿using UnityEngine;
using ZCUtil.DoubleBuffer;
using ZCUtil.Touch;

namespace ZCTouchDrawBoard
{
    /// <summary>
    /// 画笔基类
    /// </summary>
    [SerializeField]
    public class BasePaint
    {
        #region ProtectedFiled
        [SerializeField] protected Material m_Shader;
        [SerializeField] protected bool m_IsDrawing = true;
        [SerializeField] protected bool m_IsPreNull = true;
        [SerializeField] protected Vector3 m_PrePoint = Vector3.zero;
        [SerializeField] protected float m_TotalLineSpace = 0.0f;
        [SerializeField] protected float m_TotalLineLength = 0.0f;
        [SerializeField] protected PaintAttribute m_PaintModel;
        protected DoublePaper m_DoublePaper;
        protected Texture2D[] m_Channels = new Texture2D[16];
        #endregion

        #region PublicMethod
        public virtual PaintAttribute PaintAttribute
        {
            get => m_PaintModel;
            set
            {
                m_PaintModel = value;
            }
        }
        #endregion

        #region PublicMethod
        public BasePaint(string shaderName)
        {
            m_PaintModel = new PaintAttribute();
            m_Shader = new Material(Shader.Find(shaderName));
            m_DoublePaper = TouchDrawingController.Instance.DoublePaper;
        }

        public BasePaint(PaintAttribute paintModel, string shaderName)
        {
            m_PaintModel = paintModel;
            m_Shader = new Material(Shader.Find(shaderName));
            m_DoublePaper = TouchDrawingController.Instance.DoublePaper;
        }

        public virtual void DrawToPaper(TouchPoint touch, BasePaperView paper, ePaintEvent paintEvent, Mesh mesh = null)
        {
            Vector3[] positions = touch.Points;
            DrawToPaper(positions, paper, mesh);
        }

        public virtual void DrawToPaper(Vector3[] positions, BasePaperView paper, Mesh mesh = null) 
        {

        }

        protected bool CanDrawing(Vector3 point)
        {
            if (PaintAttribute.LineSpace <= 0.0f)
            {
                return true;
            }
            if (m_IsPreNull)
            {
                m_PrePoint = point;
                m_IsPreNull = false;
            }
            float distance = Vector3.Distance(m_PrePoint, point);
            if (m_IsDrawing)
            {
                // 统计lineLength
                m_TotalLineLength += distance;
                if (m_TotalLineLength > PaintAttribute.LineLength)
                {
                    m_IsDrawing = false;
                    m_TotalLineSpace = 0.0f;
                }
            }
            else
            {
                // 统计lineSpcae
                m_TotalLineSpace += distance;
                if (m_TotalLineSpace > PaintAttribute.LineSpace)
                {
                    m_IsDrawing = true;
                    m_TotalLineLength = 0.0f;
                }
            }
            m_PrePoint = point;
            return m_IsDrawing;
        }
        #endregion
    }
}
